import { RouteRecordRaw } from "vue-router";

const routes: RouteRecordRaw[] = Object.entries(
  import.meta.glob(["../views/**/index.vue"])
).reduce((prev, [path, mod]) => {
  let name = path
    .replace("../views/", "")
    .replace("index.vue", "")
    .replace(/(.*)\//, "$1");


  if (name.includes("/")) {
    const names = name.split("/");
    if (prev && !prev.length) {
      const children = [];
      children.push({
        name: names[1],
        path: `${names[1]}`,
        url: `/${name}`,
        component: mod,
      });
      prev.push({
        name: names[0],
        path: `/${names[0]}`,
        children,
      });
    } else {
      prev.forEach((item) => {
        if (item.name === names[0]) {
          item.children.push({
            name: names[1],
            path: `${names[1]}`,
            component: mod,
            url: `/${name}`,
          });
        } else {
          const children = [];
          children.push({
            name: names[1],
            path: `${names[1]}`,
            component: mod,
            url: `/${name}`,
          });
          prev.push({
            name: names[0],
            path: `/${names[0]}`,
            children,
          });
        }
      });
    }
  } else {
    prev.push({
      name: name || "home",
      path: `/${name}`,
      url: `/${name}`,
      component: mod,
    });
  }
  return prev;
}, []);
export default routes;
